//
//  WBRefreshControl.swift
//  Weibo
//
//  Created by 付启刚 on 15/12/23.
//  Copyright © 2015年 faq. All rights reserved.
//

import UIKit
import SnapKit
enum WBRefreshStatue: Int {
    // 默认状态
    case Normal = 0
    // 用户下拉到临界值 还没有松手刷新的状态
    case Pulling = 1
    // 正在刷新的状态
    case Refreshing = 2
}

//刷新控件的高度
private let RefreshViewHeight: CGFloat = 60

class WBRefreshControl: UIControl {
    
    //定义属性 保存上一次刷新的状态
    var oldStatue: WBRefreshStatue = .Normal
    
    // MARK:定义外部模型属性
    var statue: WBRefreshStatue = .Normal {
        didSet {
            //更具不同的状态 更改显示的逻辑
            switch statue {
            case .Pulling:
                print("准备起飞")
                //1.修改提示文案
                tipLabel.text = "准备起飞"
                //2.旋转箭头
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.arrowIcon.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
                })
            case .Refreshing:
                print("正在起飞")
                tipLabel.text = "正在起飞..."
                //隐藏箭头 显示转动的小菊花
                arrowIcon.hidden = true
                loadIcon.startAnimating()
                //应该主动触发 control 的方法响应事件
                //修该contentInset
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    var inset = self.scrollView!.contentInset
                    inset.top += RefreshViewHeight
                    self.scrollView?.contentInset = inset
                })
                
                sendActionsForControlEvents(.ValueChanged)
            case .Normal:
                //回到从前  最初的状态  重置
                print("下拉起飞")
                tipLabel.text = "下拉起飞"
                arrowIcon.hidden = false
                loadIcon.stopAnimating()
                
                //修该contentInset  inset.Top 会被多减一次
                if oldStatue == .Refreshing {
                    UIView.animateWithDuration(0.25, animations: { () -> Void in
                        var inset = self.scrollView!.contentInset
                        inset.top -= RefreshViewHeight
                        self.scrollView?.contentInset = inset
                    })
                }
                
                
                //重置箭头方向
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.arrowIcon.transform = CGAffineTransformIdentity
                })
            }
            
            
            //所有的设置结束后 保存上一次的刷新状态
            oldStatue = statue
        }
    }
    
    //实现kvo方法
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        //        print(change)
        //        print(scrollView?.contentInset)
        //1.获取tableView的 临界值
        let insetTop = scrollView?.contentInset.top ?? 0
        let condationValue = -(RefreshViewHeight + insetTop)
        //2.获取tableView 的 contentOffset
        let offsetY = scrollView?.contentOffset.y ?? 0
        //        print(condationValue)
        //        print(offsetY)
        
        if scrollView!.dragging {
            if statue == .Normal && offsetY < condationValue {
                //进入用户下拉到临界值 还没有松手刷新的状态
                statue = .Pulling
            } else if statue == .Pulling && offsetY > condationValue  {
                //进入默认状态
                statue = .Normal
            }
        } else {
            // 不是在被拉拽的状态 并且 当前的视图的statue 是 pulling状态 再执行刷新操作
            if statue == .Pulling {
                statue = .Refreshing
            }
        }
        
        //3.比较对应的临界值和位移的大小
    }
    
    func endRefreshing() {
        let delta = dispatch_time(DISPATCH_TIME_NOW, Int64(1) * Int64(NSEC_PER_SEC))
        dispatch_after(delta, dispatch_get_main_queue()) { () -> Void in
            self.statue = .Normal
        }
        
    }
    
    //view的生命周期方法
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        //可以获取到当前控件的父视图
        //1.获取tableView
        if let myFather = newSuperview as? UIScrollView {
            //如果父视图能够转化为 滚动视图
            //观察 滚动视图的contentOffset
            self.scrollView = myFather
            //设置kvo
            self.scrollView?.addObserver(self, forKeyPath: "contentOffset", options: .New, context: nil)
        }
    }
    
    //移除kvo观察
    deinit {
        self.scrollView?.removeObserver(self, forKeyPath: "contentOffset")
    }
    
    
    
    
    // MARK:重写构造方法
    override init(frame: CGRect) {
        //在视图内部制定frame
        let rect = CGRect(x: 0, y: -RefreshViewHeight, width: screenW, height: RefreshViewHeight)
        super.init(frame: rect)
        setupUI()
        //设置颜色
        backgroundColor = UIColor.radomColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:设置页面 和 布局
    private func setupUI() {
        //添加子视图
        addSubview(tipLabel)
        addSubview(arrowIcon)
        addSubview(loadIcon)
        
        //设置约束
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX).offset(15)
            make.centerY.equalTo(self.snp_centerY)
        }
        
        arrowIcon.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_left).offset(-5)
            make.centerY.equalTo(tipLabel.snp_centerY)
            
        }
        loadIcon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(arrowIcon)
        }
        
    }
    
    //定义属性 记录父视图
    var scrollView: UIScrollView?
    
    // MARK:懒加载所有的子视图
    private lazy var arrowIcon: UIImageView = UIImageView(image: UIImage(named: "tableview_pull_refresh"))
    private lazy var tipLabel: UILabel = UILabel(title: "下拉起飞", color: UIColor.radomColor(), fontSize: 14)
    private lazy var loadIcon: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
    
}
